﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using AgileBoard.Domain;
using AgileBoard.Dtos;
using AgileBoard.Dtos.Mappers;
using AgileBoard.Web.Controllers.Util;

namespace AgileBoard.Web.Controllers
{
    public class ReleaseController : BaseController
    {
        public ActionResult Index(Guid projectId)
        {
            var rdto = new ResultDTO();

            //NHibernate works more with interface, so creating creating instance of IList interface and assign it with List
            IList releases = ManagerFactory.ReleaseManager.GetByProject(projectId);
            DateTime labelDate = DateTime.Now;

            // add one default release, if not found
            if (releases.Count == 0)
            {
                //create default release
                Release release = ManagerFactory.ReleaseManager.NewRelease();
                release.ProjectID = projectId;

                //create default feature
                Feature feature = ManagerFactory.FeatureManager.NewFeature();
                feature = ManagerFactory.FeatureManager.Save(feature);
                release.AddFeature(feature);

                //create default iteration
                Iteration iteration = ManagerFactory.IterationManager.NewIteration();
                iteration = ManagerFactory.IterationManager.Save(iteration);
                release.AddIteration(iteration);

                //we also want to create iteration labels, i mean working days
                base.CreateLabels(DateTime.Now, iteration.IterationID);

                release = ManagerFactory.ReleaseManager.Save(release);
                feature.ReleaseID = release.ReleaseID.ToString();
                iteration.ReleaseID = release.ReleaseID;
                releases.Add(release); //now add to list so that default one will get display in UI
            }

            IList<ReleaseDTO> releaseDTO = new List<ReleaseDTO>();
            foreach (Release release in releases)
            {
                ReleaseDTO releaseDto = ReleaseDTOMapper.MapToDTO(release);
                releaseDTO.Add(releaseDto);
            }

            rdto.Result = true;
            rdto.Message = "Success";
            //replace JSON inner collection name
            rdto.Data = releaseDTO.ToExtJsJson("items", "totalCount");

            return Json(rdto);
        }

        public ActionResult Update(ReleaseDTO Json, Boolean Update)
        {
            var rdto = new ResultDTO();
            Release newRelease = ReleaseDTOMapper.MapFromDTO(Json);

            if (!Update)
            {
                //initialize new release 
                newRelease.Name = "New Release";
                newRelease.PeriodFrom = newRelease.PeriodTo.AddDays(1);
                newRelease.PeriodTo = newRelease.PeriodFrom.AddDays(30);
                newRelease.CreateTime = DateTime.Now;

                //create default feature for release
                Feature newFeature = ManagerFactory.FeatureManager.NewFeature();
                newFeature.Target = newRelease.PeriodTo;
                ManagerFactory.FeatureManager.Save(newFeature);
                newRelease.AddFeature(newFeature);
            }

            try
            {
                foreach (Iteration iteration in newRelease.Iterations)
                {
                    if (DateTime.Compare(newRelease.PeriodFrom.Date, iteration.PeriodFrom.Date) > 0
                        || DateTime.Compare(newRelease.PeriodTo.Date, iteration.PeriodTo.Date) < 0
                        )
                    {
                        rdto.Message = String.Format("Release period should include Iteration dates({0} - {1})",
                                                     iteration.PeriodFrom, iteration.PeriodTo);
                        rdto.Result = false;
                        return this.Json(rdto);
                    }
                }

                foreach (Feature feature in newRelease.Features)
                {
                    feature.Target = newRelease.PeriodTo;
                }

                newRelease = ManagerFactory.ReleaseManager.Update(newRelease);
                rdto.Message = "Success";
                rdto.Result = true;
                rdto.Data = ReleaseDTOMapper.MapToDTO(newRelease).ToExtJsJson();
            }
            catch (Exception e)
            {
                rdto.Message = e.Message;
                rdto.Result = false;
            }

            return this.Json(rdto);
        }

        public ActionResult Delete(Guid Guid)
        {
            var rdto = new ResultDTO();
            try
            {
                if (ManagerFactory.IterationManager.GetByRelease(Guid).Count > 0)
                    throw new Exception(
                        "Release which contains iterations cannot be removed, Remove iterations and try again.");

                ManagerFactory.ReleaseManager.Delete(Guid);
                rdto.Message = "Success";
                rdto.Result = true;
            }
            catch (Exception ex)
            {
                rdto.Message = ex.Message;
                rdto.Result = false;
            }
            return Json(rdto);
        }
    }
}